home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / file_util.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  6.7 KB  |  246 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''distutils.file_util
  5.  
  6. Utility functions for operating on single files.
  7. '''
  8. __revision__ = '$Id: file_util.py 37828 2004-11-10 22:23:15Z loewis $'
  9. import os
  10. from distutils.errors import DistutilsFileError
  11. from distutils import log
  12. _copy_action = {
  13.     None: 'copying',
  14.     'hard': 'hard linking',
  15.     'sym': 'symbolically linking' }
  16.  
  17. def _copy_file_contents(src, dst, buffer_size = 16384):
  18.     """Copy the file 'src' to 'dst'; both must be filenames.  Any error
  19.     opening either file, reading from 'src', or writing to 'dst', raises
  20.     DistutilsFileError.  Data is read/written in chunks of 'buffer_size'
  21.     bytes (default 16k).  No attempt is made to handle anything apart from
  22.     regular files.
  23.     """
  24.     fsrc = None
  25.     fdst = None
  26.     
  27.     try:
  28.         fsrc = open(src, 'rb')
  29.     except os.error:
  30.         (errno, errstr) = None
  31.         raise DistutilsFileError, "could not open '%s': %s" % (src, errstr)
  32.     
  33.  
  34.     if os.path.exists(dst):
  35.         
  36.         try:
  37.             os.unlink(dst)
  38.         except os.error:
  39.             (errno, errstr) = None
  40.             raise DistutilsFileError, "could not delete '%s': %s" % (dst, errstr)
  41.         except:
  42.             None<EXCEPTION MATCH>os.error
  43.         
  44.  
  45.     None<EXCEPTION MATCH>os.error
  46.     
  47.     try:
  48.         fdst = open(dst, 'wb')
  49.     except os.error:
  50.         (errno, errstr) = None
  51.         raise DistutilsFileError, "could not create '%s': %s" % (dst, errstr)
  52.  
  53.     while None:
  54.         
  55.         try:
  56.             buf = fsrc.read(buffer_size)
  57.         except os.error:
  58.             (errno, errstr) = None
  59.             raise DistutilsFileError, "could not read from '%s': %s" % (src, errstr)
  60.  
  61.         if not buf:
  62.             break
  63.         
  64.         
  65.         try:
  66.             fdst.write(buf)
  67.         continue
  68.         except os.error:
  69.             (errno, errstr) = None
  70.             raise DistutilsFileError, "could not write to '%s': %s" % (dst, errstr)
  71.             continue
  72.         
  73.  
  74.     if fsrc:
  75.         fsrc.close()
  76.     
  77.  
  78.  
  79. def copy_file(src, dst, preserve_mode = 1, preserve_times = 1, update = 0, link = None, verbose = 0, dry_run = 0):
  80.     '''Copy a file \'src\' to \'dst\'.  If \'dst\' is a directory, then \'src\' is
  81.     copied there with the same name; otherwise, it must be a filename.  (If
  82.     the file exists, it will be ruthlessly clobbered.)  If \'preserve_mode\'
  83.     is true (the default), the file\'s mode (type and permission bits, or
  84.     whatever is analogous on the current platform) is copied.  If
  85.     \'preserve_times\' is true (the default), the last-modified and
  86.     last-access times are copied as well.  If \'update\' is true, \'src\' will
  87.     only be copied if \'dst\' does not exist, or if \'dst\' does exist but is
  88.     older than \'src\'.
  89.  
  90.     \'link\' allows you to make hard links (os.link) or symbolic links
  91.     (os.symlink) instead of copying: set it to "hard" or "sym"; if it is
  92.     None (the default), files are copied.  Don\'t set \'link\' on systems that
  93.     don\'t support it: \'copy_file()\' doesn\'t check if hard or symbolic
  94.     linking is available.
  95.  
  96.     Under Mac OS, uses the native file copy function in macostools; on
  97.     other systems, uses \'_copy_file_contents()\' to copy file contents.
  98.  
  99.     Return a tuple (dest_name, copied): \'dest_name\' is the actual name of
  100.     the output file, and \'copied\' is true if the file was copied (or would
  101.     have been copied, if \'dry_run\' true).
  102.     '''
  103.     newer = newer
  104.     import distutils.dep_util
  105.     ST_ATIME = ST_ATIME
  106.     ST_MTIME = ST_MTIME
  107.     ST_MODE = ST_MODE
  108.     S_IMODE = S_IMODE
  109.     import stat
  110.     if not os.path.isfile(src):
  111.         raise DistutilsFileError, "can't copy '%s': doesn't exist or not a regular file" % src
  112.     os.path.isfile(src)
  113.     if os.path.isdir(dst):
  114.         dir = dst
  115.         dst = os.path.join(dst, os.path.basename(src))
  116.     else:
  117.         dir = os.path.dirname(dst)
  118.     if update and not newer(src, dst):
  119.         log.debug('not copying %s (output up-to-date)', src)
  120.         return (dst, 0)
  121.     
  122.     try:
  123.         action = _copy_action[link]
  124.     except KeyError:
  125.         not newer(src, dst)
  126.         not newer(src, dst)
  127.         raise ValueError, "invalid value '%s' for 'link' argument" % link
  128.     except:
  129.         not newer(src, dst)
  130.  
  131.     if os.path.basename(dst) == os.path.basename(src):
  132.         log.info('%s %s -> %s', action, src, dir)
  133.     else:
  134.         log.info('%s %s -> %s', action, src, dst)
  135.     if dry_run:
  136.         return (dst, 1)
  137.     if os.name == 'mac':
  138.         import macostools
  139.         
  140.         try:
  141.             macostools.copy(src, dst, 0, preserve_times)
  142.         except os.error:
  143.             dry_run
  144.             exc = dry_run
  145.             raise DistutilsFileError, "could not copy '%s' to '%s': %s" % (src, dst, exc[-1])
  146.         except:
  147.             dry_run<EXCEPTION MATCH>os.error
  148.         
  149.  
  150.     dry_run
  151.     if link == 'hard':
  152.         if not os.path.exists(dst) and os.path.samefile(src, dst):
  153.             os.link(src, dst)
  154.         
  155.     elif link == 'sym':
  156.         if not os.path.exists(dst) and os.path.samefile(src, dst):
  157.             os.symlink(src, dst)
  158.         
  159.     else:
  160.         _copy_file_contents(src, dst)
  161.         if preserve_mode or preserve_times:
  162.             st = os.stat(src)
  163.             if preserve_times:
  164.                 os.utime(dst, (st[ST_ATIME], st[ST_MTIME]))
  165.             
  166.             if preserve_mode:
  167.                 os.chmod(dst, S_IMODE(st[ST_MODE]))
  168.             
  169.         
  170.     return (dst, 1)
  171.  
  172.  
  173. def move_file(src, dst, verbose = 0, dry_run = 0):
  174.     """Move a file 'src' to 'dst'.  If 'dst' is a directory, the file will
  175.     be moved into it with the same name; otherwise, 'src' is just renamed
  176.     to 'dst'.  Return the new full name of the file.
  177.  
  178.     Handles cross-device moves on Unix using 'copy_file()'.  What about
  179.     other systems???
  180.     """
  181.     exists = exists
  182.     isfile = isfile
  183.     isdir = isdir
  184.     basename = basename
  185.     dirname = dirname
  186.     import os.path
  187.     import errno
  188.     log.info('moving %s -> %s', src, dst)
  189.     if dry_run:
  190.         return dst
  191.     if not isfile(src):
  192.         raise DistutilsFileError, "can't move '%s': not a regular file" % src
  193.     isfile(src)
  194.     if isdir(dst):
  195.         dst = os.path.join(dst, basename(src))
  196.     elif exists(dst):
  197.         raise DistutilsFileError, "can't move '%s': destination '%s' already exists" % (src, dst)
  198.     
  199.     if not isdir(dirname(dst)):
  200.         raise DistutilsFileError, "can't move '%s': destination '%s' not a valid path" % (src, dst)
  201.     isdir(dirname(dst))
  202.     copy_it = 0
  203.     
  204.     try:
  205.         os.rename(src, dst)
  206.     except os.error:
  207.         (num, msg) = None
  208.         if num == errno.EXDEV:
  209.             copy_it = 1
  210.         else:
  211.             raise DistutilsFileError, "couldn't move '%s' to '%s': %s" % (src, dst, msg)
  212.         num == errno.EXDEV
  213.  
  214.     if copy_it:
  215.         copy_file(src, dst)
  216.         
  217.         try:
  218.             os.unlink(src)
  219.         except os.error:
  220.             (num, msg) = None
  221.             
  222.             try:
  223.                 os.unlink(dst)
  224.             except os.error:
  225.                 pass
  226.  
  227.             raise DistutilsFileError, ("couldn't move '%s' to '%s' by copy/delete: " + "delete '%s' failed: %s") % (src, dst, src, msg)
  228.         except:
  229.             None<EXCEPTION MATCH>os.error
  230.         
  231.  
  232.     None<EXCEPTION MATCH>os.error
  233.     return dst
  234.  
  235.  
  236. def write_file(filename, contents):
  237.     """Create a file with the specified name and write 'contents' (a
  238.     sequence of strings without line terminators) to it.
  239.     """
  240.     f = open(filename, 'w')
  241.     for line in contents:
  242.         f.write(line + '\n')
  243.     
  244.     f.close()
  245.  
  246.